<template>
	<view class="page">
		<!-- 设置列表 -->
		<view class="setting-list">
			<view class="list">
				<view class="title">
					<text>配资模式</text>
				</view>
				<view class="more-content">
					<text class="content right">{{details.type}}</text><!--multiple=1:天；2:周；3:月；5:免息-->
				</view>
			</view>
			<view class="list">
				<view class="title">
					<text>操盘状态</text>
				</view>
				<view class="more-content">
					<text class="content right">{{details.status}}</text>
				</view>
			</view>
			<view class="list">
				<view class="title">
					<text>交易账户</text>
				</view>
				<view class="more-content">
					<text class="content right red text-bold">{{details.sub_account}}</text>
				</view>
			</view>
			<view class="list">
				<view class="title">
					<text>申请单号</text>
				</view>
				<view class="more-content">
					<text class="content right">{{details.order_id}}</text>
				</view>
			</view>
			<view class="list">
				<view class="title">
					<text>操盘期限</text>
				</view>
				<view class="more-content">
					<text class="content right">{{details.verify_time}}至{{details.end_time}}</text>
				</view>
			</view>
			<view class="list">
				<view class="title">
					<text>自动续期</text>
				</view>
				<view class="more-content" v-if="details.type_code==1||details.type_code==2||details.type_code==3">
					<switch class="red sm" color="red !important" :checked="treatmentStatu.checked" @change="switchChange" style="transform:scale(0.8)"></switch>
					<!--text class="content left">{{details.renewal==1 ? '开启':'关闭'}}</text-->
					<text class="content right" @click="onSetting('renewal',id)"><span>申请延期</span></text>
					<text class="iconfont icon-more more"></text>
				</view>
			</view>
			<view class="list">
				<view class="title">
					<text>保证金</text>
				</view>
				<view class="more-content">
					<text class="content left">{{calculate(details.deposit_money)}}</text>
					<text class="content right"
					 v-if="details.type_code==1||details.type_code==2||details.type_code==3"
					 @click="onSetting('addmoney',id)"><span>追加资金</span></text>
					<text class="iconfont icon-more more"></text>
				</view>
			</view>
			<view class="list">
				<view class="title">
					<text>总操盘资金</text>
				</view>
				<view class="more-content">
					<text class="content left">{{calculate(details.init_money)}}</text>
					<text class="content right"
					v-if="details.type_code==1||details.type_code==2||details.type_code==3"
					@click="onSetting('expend',id)"><span>扩大配资</span></text>
					<text class="iconfont icon-more more"></text>
				</view>
			</view>
			<view class="list">
				<view class="title">
					<text>预计盈亏</text>
				</view>
				<view class="more-content">
					<text class="content left">{{calculate(details.return_money)}}</text>
					<text class="content right"
					v-if="details.type_code==1||details.type_code==2||details.type_code==3"
					@click="onSetting('profit',id)"><span>提取盈利</span></text>
					<text class="iconfont icon-more more"></text>
				</view>
			</view>
			<view class="list">
				<view class="title">
					<text>警戒线</text>
				</view>
				<view class="more-content">
					<text class="content left">{{calculate(details.loss_warn_money)}}</text>
				</view>
			</view>
			<view class="list">
				<view class="title">
					<text>平仓线</text>
				</view>
				<view class="more-content">
					<text class="content left">{{calculate(details.loss_close_money)}}</text>
				</view>
			</view>
			<view class="list">
				<view class="title">
					<text>总利息</text>
				</view>
				<view class="more-content">
					<text class="content left">{{calculate(details.borrow_interest)}}</text>
				</view>
			</view>
			<view class="list" @click="onSetting('account')">
				<view class="title">
					<text>总资产</text>
				</view>
				<view class="more-content">
					<text class="content left red">{{calculate(accountInfo.total_money)}}</text>
				</view>
			</view>
			<view class="list">
				<view class="title">
					<text>查看合同</text>
				</view>
				<view class="more-content">
					<text class="content right"></text>
					<text class="content right" @click="onSetting('agreement')">《平台操盘协议》</text>
				</view>
			</view>
		</view>
		<!-- 退出 -->
		<view class="quit-login" v-if="details.status=='未通过'">
			<view class="btn"><text >未通过</text></view>
		</view>
		<view class="quit-login" v-else @click="onStot(token,id)">
			<view class="btn"><text>终止操盘</text></view>
		</view>
		<view class="bottom"></view>
		<!-- 提示框 -->
		<DialogBox ref="DialogBox"></DialogBox>
	</view>
</template>

<script>
	var _self,loginRes;
	export default {
		data() {
			return {
				myMobile: '',
				details: {},
				accountInfo:uni.getStorageSync('accountInfo'),
				token: '',
				id: '',
				treatmentStatu: { checked: false },
			};
		},
		onLoad(params){
			//console.log(params.token+" "+params.id)
			_self = this;
			loginRes = this.checkLogin();
			if(!loginRes){return;}
			this.token = params.token;
			this.id    = params.id;
			this.getDetails(params.token,params.id);
			//this.getacInfo(params.token,params.id);
		},
		methods:{
			/*获取操盘账户相关信息*/
			async getDetails(token,id){
				uni.request({
					url: this.apiServer+'/apicom/financing/details?id='+id,
					header: {'content-type' : "application/x-www-form-urlencoded"},
					method: 'POST',
					timeout: 5000,
					data:{
						token: token
					},
					success: res => {
						if(res.data.status == 1){
							this.details  = res.data.data.result;
							if(res.data.data.result.renewal==1){
								this.$set(this.treatmentStatu, 'checked', true)
							}else{
								this.$set(this.treatmentStatu, 'checked', false)
							}
							//console.log(this.details);
						}
					},
					fail:function(e){
						uni.showToast({title:"加载失败!",icon:"none"});
					}
				});
			},
			/*获取会员资金相关信息*/
			async getacInfo(token,id){
				if(token=='' || id==''){return;}
				uni.request({
					url: this.apiServer+'/market/trade/account_info',
					header: {'content-type' : "application/x-www-form-urlencoded"},
					method: 'POST',
					timeout: 5000,
					data:{
						token: token,
						id:    id,
					},
					success: res => {
						if(res.data.status == 1){
							this.accountInfo  = res.data.data;
						}
					},
					fail:function(e){
						uni.showToast({title:"加载失败!",icon:"none"});
					},
				});
			},
			/*自动续期按钮*/
			switchChange (e) {
				let value = e.target.value
				let that = this
				this.$set(this.treatmentStatu, 'checked', value)   // 将点击改变的状态赋给treatmentStatu.checked
				uni.request({
					url: this.apiServer+'/apicom/financing/autorenewal',
					header: {'content-type' : "application/x-www-form-urlencoded"},
					method: 'POST',
					timeout: 5000,
					data:{
						id   : this.details.id,
						token: this.token
					},
					success: res => {
						if(res.data.status != 1){
							//this.renewal  = res.data.data;
							that.$set(that.treatmentStatu, 'checked', false)  // 手动修改switch的状态，视图会同步更新
							//console.log(res.data.message);
						}else{
							that.$emit('changePage', 1)
							//console.log(res.data.message);
						}
					},
					fail:function(e){
						uni.showToast({title:"加载失败!",icon:"none"});
					}
				});
			},
			/**
			 * 设置列表点击
			 * @param {String} type
			 */
			onSetting(type,id){
				//console.log(type);
				switch(type) {
					case 'renewal':
						uni.navigateTo({
							url: '/pages/Renewal/Renewal?id='+id
						})
						break;
					case 'addmoney':
						uni.navigateTo({
							url: '/pages/Addmoney/Addmoney?id='+id
						})
						break;
					case 'expend':
						uni.navigateTo({
							url: '/pages/Expend/Expend?id='+id
						})
						break;
					case 'profit':
						uni.navigateTo({
							url: '/pages/Profit/Profit?id='+id
						})
						break;
					case 'agreement':
						uni.navigateTo({
							url: '/pages/Agreement/Agreement?id='+id
						})
						break;
				}
			},
			/*终止操盘点击*/
			onStot(token,id){
				this.$refs['DialogBox'].confirm({
					title: '提示',
					content: '是否要终止操盘?',
					DialogType: 'inquiry',
					animation: 0
				}).then(()=>{
					uni.request({
						url: this.apiServer+'/apicom/financing/stop',
						header: {'content-type' : "application/x-www-form-urlencoded"},
						method: 'POST',
						timeout: 5000,
						data:{
							token: token,
							id   : id,
						},
						success: res => {
							if(res.data.status == 1){
								uni.showToast({title:res.data.message,icon:"none"});
							}else{
								uni.showToast({title:res.data.message,icon:"none"});
							}
						},
						complete:function(){
						    uni.stopPullDownRefresh();
						},
						fail:function(e){
							uni.showToast({title:"加载失败!",icon:"none"});
						}
					});
				})
			},
			/*计算单位*/
			calculate(number){
				//console.log(Math.abs(parseInt(number)));return;
				if(Math.abs(parseInt(number))==0)return number;
				if(Math.abs(parseInt(number)).toString().length >= 13){
					var unit = (parseInt(number)/1000000000000).toFixed(2) + '万亿';
					return unit;
				}else if(Math.abs(parseInt(number)).toString().length >= 9){
					var unit = (parseInt(number)/100000000).toFixed(2) + '亿';
					return unit;
				}else if(Math.abs(parseInt(number)).toString().length > 4){
					var unit =  (parseInt(number)/10000).toFixed(2) + '万';
					return unit;
				}else{
					var unit =  parseInt(number).toFixed(2);
					return unit;
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	@import 'AccoutSet.scss';
</style>
